package com.tjbank.cssys.web.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.tjbank.cssys.repository.CkUserMapper;
import com.tjbank.cssys.web.config.Constant;
import com.tjbank.cssys.web.service.CkUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class CkUserServiceImpl implements CkUserService {

    private static Logger logger = LoggerFactory.getLogger(CkUserServiceImpl.class);


    @Autowired
    private CkUserMapper ckUseMapper;



    @Override
    public HashMap<String, Object> getUserBranchAndSub(String user_code) {
        HashMap<String,Object> result = new HashMap<>();

        HashMap<String, String> params = new HashMap<String, String>();
        params.put("user_code",user_code);
        List<HashMap<String, String>> organInfo = ckUseMapper.getOrganInfoByUserCode(params);
        if(null!=organInfo && organInfo.size()>0){
            HashMap<String, String>  mm= organInfo.get(0);
            if(mm.get("ID").equals(Constant.ROOTCODE)){

            }else if(mm.get("PARENT_ID").equals(Constant.ROOTCODE)){
                result.put("branch",mm);
                result.put("subBranch",null);
            }else{
                HashMap<String, String> branchOrgan = getBranchOrgan(mm);
                result.put("branch",branchOrgan);
                if(null!=branchOrgan){
                    if(mm.get("PARENT_ID").equals(branchOrgan.get("ID"))){

                        result.put("subBranch",mm);
                    }else{
                        HashMap<String, String> subBranchOrgan = getSubBranchOrgan(mm, branchOrgan.get("ID"));
                        result.put("subBranch",subBranchOrgan);
                    }



                }
            }
        }


        return result;
    }

    //获取分行信息
    private HashMap<String, String> getBranchOrgan(HashMap<String, String> params){
        List<HashMap<String, String>> parentOrgan = ckUseMapper.getParentOrgan(params);
        if(null!=parentOrgan && parentOrgan.size()>0){
            HashMap<String, String> mm = parentOrgan.get(0);
            System.out.println("parentOrgan==="+ JSONObject.toJSONString(parentOrgan));
            if(mm.get("PARENT_ID").equals(Constant.ROOTCODE)){
                return mm;
            }else{
                return getBranchOrgan(mm);
            }
        }else{
            return null;
        }

    }

    //获取支行信息
    private HashMap<String, String> getSubBranchOrgan(HashMap<String, String> params,String branch_ID){
        List<HashMap<String, String>> parentOrgan = ckUseMapper.getParentOrgan(params);
        if(null!=parentOrgan && parentOrgan.size()>0){
            HashMap<String, String> mm = parentOrgan.get(0);
            if(mm.get("PARENT_ID").equals(branch_ID)){
                return mm;
            }else{
                return getSubBranchOrgan(mm,branch_ID);
            }
        }else{
            return null;
        }
    }
}
